<?php

declare(strict_types=1);

namespace app\model;

use app\common\model\Goods as payGoods;
use app\third\controller\Pay;
use think\model\concern\SoftDelete;
use think\facade\Db;
/**
 * 
 *
 * @mixin \think\Model
 * @property int $id 自增id
 * @property int $agent_id 代理商id
 * @property int $order_id 订单号
 * @property string $params 下单参数
 * @property string $result 下单返回
 * @property int $status 状态：1未支付 2已支付待下单 3已下单 4下单失败
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class TaototoOrder extends CommonModel
{
    protected static $statusText=[
        '','待下单','受理中','待出票','已出票待结算','已结算-守兔','待结算-系统','已结算-系统','','','订单关闭'
    ];

    /**
     *  订单列表
     *  type 1 管理员查看   2代理商查看
     */
       public static function getList($params,$type=1){
            if($type == 2 && empty($params['agent_id'])){
               return fail('账号出错！');
           }
             $where=self::setWhere($params);
              $paginate = [
                  'page' => $params['page'] ?? 1,
                  'list_rows' => $params['list_rows'] ?? 10,
              ];
            $db = self::alias("a")
                ->field("a.*,c.mobile,d.username as agent_name,e.name as supplier_name")
                ->join("dmj_users c","a.user_id =c.id","left")
                ->join("agent d","d.id =a.agent_id","left")
                ->join("mobile_supplier e","e.id =a.supplier_id","left")
                ->where($where)->order('a.id desc');
            if (isset($params['is_export']) && 1 == $params['is_export']) {
                //返回导出excel 需要的数据
                $list['data'] = $db->select()->toArray();
            } else {
                $list = $db->paginate($paginate)->toArray();
            }
           foreach ($list['data'] as &$nv){
               $nv['status_text']=self::$statusText[$nv['status']];
               $nv['amount_y']=fen2yuan($nv['amount']);
               $nv['pay_amount_y']=fen2yuan($nv['pay_amount']);
               //平台利润=代理商结算价-供应商-通道费
               $nv['profit']=$nv['agent_amount']-$nv['supplier_amount'];
               $nv['profit']=$nv['profit']-$nv['supplier_gate_amount'];
               $nv['profit']=fen2yuan($nv['profit']);
               //代理利润=总价-代理结算价
               $nv['agent_profit']=fen2yuan($nv['amount']-$nv['agent_amount']);
           }
           if (isset($params['is_export']) && 1 == $params['is_export']) {
               return $list['data'];
           }
            return $list;
       }

    public static function setWhere($data){
        $where=[];
        if(isset($data['status']) && !empty($data['status'])){
            if($data['status'] == 5) $where[]=['status','in','5,6,7'];
            else  $where[]=['a.status','=',$data['status']];
        }
        if(isset($data['order_id']) && !empty($data['order_id'])){
                $where[]=['a.order_id|a.supplier_order_id',"like","%".$data['order_id']."%"];
        }
        if(isset($data['user']) && !empty($data['user'])){
                 $where[]=['a.user_id|c.name','like','%'.$data['user'].'%'];
        }
        if(isset($data['agent_name']) && !empty($data['agent_name']))  $where[]=['d.username','like','%'.$data['agent_name'].'%'];
        if(isset($data['supplier_name']) && !empty($data['supplier_name']))  $where[]=['e.name','like','%'.$data['supplier_name'].'%'];
        if(isset($data['agent_id']) && !empty($data['agent_id']))  $where[]=['a.agent_id','=',$data['agent_id']];
        $createdDate = $data['createdDate'] ?? [];
        if (is_array($createdDate) && !empty($createdDate[0]) && $createdDate[1]) {
            $where[] = ['a.created_at', 'between time', [$createdDate[0], $createdDate[1].' 23:59:59']];
        }
        return $where;
    }

    //下单
    public static function addOrder(){

    }


}
